{% extends "base.html" %}

{% block content %}
<style>
/* 暗色模式适配 */
[data-theme="dark"] .card {
    background: rgba(0, 0, 0, 0.3);
    border-color: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
}

[data-theme="dark"] .card-header {
    background: rgba(0, 0, 0, 0.2);
    border-bottom-color: rgba(255, 255, 255, 0.1);
    color: #ffffff;
}

[data-theme="dark"] .form-label {
    color: #ffffff !important;
}

[data-theme="dark"] .form-control {
    background: rgba(0, 0, 0, 0.3);
    border-color: rgba(255, 255, 255, 0.2);
    color: #ffffff;
}

[data-theme="dark"] .form-control:focus {
    background: rgba(0, 0, 0, 0.4);
    border-color: rgba(255, 255, 255, 0.3);
    color: #ffffff;
    box-shadow: 0 0 0 0.2rem rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .form-text {
    color: #bdc3c7 !important;
    font-weight: 500;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* 明亮模式按钮液态玻璃效果 */
.btn-info {
    background: rgba(255, 255, 255, 0.1) !important;
    backdrop-filter: blur(15px) !important;
    -webkit-backdrop-filter: blur(15px) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: #1C1C1E !important;
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.05),
        inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
    position: relative;
    overflow: hidden;
}

.btn-info::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.btn-info:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 
        0 6px 20px rgba(0, 0, 0, 0.1),
        0 2px 8px rgba(0, 0, 0, 0.05),
        inset 0 1px 0 rgba(255, 255, 255, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    background: rgba(255, 255, 255, 0.15) !important;
    color: #1C1C1E !important;
}

.btn-info:hover::before {
    opacity: 1;
}

[data-theme="dark"] .btn-info {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: #ffffff !important;
    backdrop-filter: blur(15px) !important;
    -webkit-backdrop-filter: blur(15px) !important;
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .btn-info::before {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.08) 0%, transparent 50%);
}

[data-theme="dark"] .btn-info:hover {
    background: rgba(0, 0, 0, 0.4) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #ffffff !important;
    box-shadow: 
        0 6px 20px rgba(0, 0, 0, 0.3),
        0 2px 8px rgba(0, 0, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.15) !important;
}

/* 明亮模式添加按钮液态玻璃效果 */
.btn-primary {
    background: rgba(255, 255, 255, 0.1) !important;
    backdrop-filter: blur(15px) !important;
    -webkit-backdrop-filter: blur(15px) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: #1C1C1E !important;
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.05),
        inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
    position: relative;
    overflow: hidden;
}

.btn-primary::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.btn-primary:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 
        0 6px 20px rgba(0, 0, 0, 0.1),
        0 2px 8px rgba(0, 0, 0, 0.05),
        inset 0 1px 0 rgba(255, 255, 255, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    background: rgba(255, 255, 255, 0.15) !important;
    color: #1C1C1E !important;
}

.btn-primary:hover::before {
    opacity: 1;
}

.btn-primary:disabled {
    background: rgba(255, 255, 255, 0.05) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
    color: #6c757d !important;
    transform: none !important;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
}

[data-theme="dark"] .btn-primary {
    background: rgba(0, 0, 0, 0.3) !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: #ffffff !important;
    backdrop-filter: blur(15px) !important;
    -webkit-backdrop-filter: blur(15px) !important;
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
}

[data-theme="dark"] .btn-primary::before {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.08) 0%, transparent 50%);
}

[data-theme="dark"] .btn-primary:hover {
    background: rgba(0, 0, 0, 0.4) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
    color: #ffffff !important;
    box-shadow: 
        0 6px 20px rgba(0, 0, 0, 0.3),
        0 2px 8px rgba(0, 0, 0, 0.2),
        inset 0 1px 0 rgba(255, 255, 255, 0.15) !important;
}

[data-theme="dark"] .btn-primary:disabled {
    background: rgba(0, 0, 0, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
    color: #6c757d !important;
}
</style>

<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">添加机器人</div>
            <div class="card-body">
                <form method="POST" action="{{ url_for('add_robot') }}" id="robotForm">
                    <div class="mb-3">
                        <label for="name" class="form-label">机器人名称</label>
                        <input type="text" class="form-control" id="name" name="name" required>
                    </div>
                    <div class="mb-3">
                        <label for="tag" class="form-label">机器人标签 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="tag" name="tag" required placeholder="例如：开发群、测试群、生产群等">
                        <div class="form-text">用于区分机器人属于哪个群组，便于管理</div>
                    </div>
                    <div class="mb-3">
                        <label for="webhook" class="form-label">Webhook地址</label>
                        <input type="text" class="form-control" id="webhook" name="webhook" required>
                    </div>
                    <div class="mb-3">
                        <label for="secret" class="form-label">加签密钥 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="secret" name="secret" required>
                        <div class="form-text">加签密钥用于消息签名，确保消息安全性</div>
                    </div>
                    <div class="mb-3">
                        <button type="button" class="btn btn-info" id="testBtn" onclick="testRobot()">
                            <i class="fas fa-paper-plane"></i> 测试发送
                        </button>
                        <span id="testResult" class="ms-2"></span>
                    </div>
                    <button type="submit" class="btn btn-primary" id="submitBtn">添加</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function testRobot() {
    const webhook = document.getElementById('webhook').value.trim();
    const secret = document.getElementById('secret').value.trim();
    const testBtn = document.getElementById('testBtn');
    const testResult = document.getElementById('testResult');
    const submitBtn = document.getElementById('submitBtn');
    
    if (!webhook) {
        testResult.innerHTML = '<span class="text-danger">请先填写Webhook地址</span>';
        return;
    }
    
    if (!secret) {
        testResult.innerHTML = '<span class="text-danger">请先填写加签密钥</span>';
        return;
    }
    
    // 禁用测试按钮，显示加载状态
    testBtn.disabled = true;
    testBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 测试中...';
    testResult.innerHTML = '';
    
    fetch('/test_robot', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            webhook: webhook,
            secret: secret
        })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            testResult.innerHTML = '<span class="text-success"><i class="fas fa-check"></i> ' + data.message + '</span>';
            submitBtn.disabled = false;
        } else {
            testResult.innerHTML = '<span class="text-danger"><i class="fas fa-times"></i> ' + data.message + '</span>';
            submitBtn.disabled = true;
        }
    })
    .catch(error => {
        testResult.innerHTML = '<span class="text-danger"><i class="fas fa-times"></i> 测试失败: ' + error.message + '</span>';
        submitBtn.disabled = true;
    })
    .finally(() => {
        // 恢复测试按钮
        testBtn.disabled = false;
        testBtn.innerHTML = '<i class="fas fa-paper-plane"></i> 测试发送';
    });
}

// 页面加载时禁用提交按钮，直到测试成功
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('submitBtn').disabled = true;
    
    // 当webhook或secret变化时，重新禁用提交按钮
    document.getElementById('webhook').addEventListener('input', function() {
        document.getElementById('submitBtn').disabled = true;
        document.getElementById('testResult').innerHTML = '';
    });
    
    document.getElementById('secret').addEventListener('input', function() {
        document.getElementById('submitBtn').disabled = true;
        document.getElementById('testResult').innerHTML = '';
    });
    
    // 当标签变化时，检查是否所有必填字段都已填写
    document.getElementById('tag').addEventListener('input', function() {
        const name = document.getElementById('name').value.trim();
        const tag = document.getElementById('tag').value.trim();
        const webhook = document.getElementById('webhook').value.trim();
        const secret = document.getElementById('secret').value.trim();
        
        if (name && tag && webhook && secret) {
            document.getElementById('submitBtn').disabled = false;
        } else {
            document.getElementById('submitBtn').disabled = true;
        }
    });
    
    // 当名称变化时，检查是否所有必填字段都已填写
    document.getElementById('name').addEventListener('input', function() {
        const name = document.getElementById('name').value.trim();
        const tag = document.getElementById('tag').value.trim();
        const webhook = document.getElementById('webhook').value.trim();
        const secret = document.getElementById('secret').value.trim();
        
        if (name && tag && webhook && secret) {
            document.getElementById('submitBtn').disabled = false;
        } else {
            document.getElementById('submitBtn').disabled = true;
        }
    });
});
</script>
{% endblock %}